package com.example.front.serivce;

import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.common.exception.BusinessException;
import com.example.front.dto.ButtonDto;
import com.example.front.dto.LevelDto;
import com.example.front.entity.Box;
import com.example.front.entity.Level;
import com.example.front.entity.MessageLogging;
import com.example.front.mapper.LevelMapper;
import com.example.front.mapper.MessageLoggingMapper;
import com.example.passport.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class MessageLoggingService {

    @Resource
    MessageLoggingMapper messageLoggingMapper;
    @Resource
    UserService userService;
    /**
     * 添加消息日志
     */
    public void createMessageLogging(MessageLogging messageLogging) {
        if (messageLogging.getUserId()==null) {
            throw new BusinessException("用户不能为空");
        }
        messageLoggingMapper.create(messageLogging);
    }



    /**
     * 列出消息日志
     */
    public ListDto<MessageLogging> listMessageLogging(MessageLogging condition, Pagination pagination) {

        List<MessageLogging> messageLoggingList = messageLoggingMapper.findAll(condition, pagination);
        List<MessageLogging> buttonDtoList = messageLoggingList.stream().map(box -> {
            MessageLogging messageLogging=new MessageLogging();
            BeanUtils.copyProperties(box, messageLogging);
            String name=userService.findNameById(messageLogging.getUserId());
            messageLogging.setUserName(name);
            return messageLogging;
        }).collect(Collectors.toList());
        return new ListDto<>(buttonDtoList, pagination);
    }

    /**
     * 删除消息日志
     */
    public void deleteLevel(long tenantId, Long id) {
        if (messageLoggingMapper.deleteById(id,tenantId) == 0) {
            throw new BusinessException("删除失败");
        }
    }

    /**
     * 查找消息日志
     */
    public MessageLogging findById(Long tenantId, Long id) {
        MessageLogging messageLogging = messageLoggingMapper.findById(tenantId, id);
        if(messageLogging==null){
            throw new BusinessException("没有日志");
        }
        return messageLogging;
    }

}
